package com.itcam.cloud.controller;


import cn.hutool.core.date.DateUtil;
import com.itcam.cloud.apis.PayFeignApi;
import com.itcam.cloud.dto.PayDTO;
import com.itcam.cloud.resp.ResultData;
import com.itcam.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/feign/pay/add")
    public ResultData addOrder(PayDTO payDTO) {
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");
        ResultData resultData = payFeignApi.addOrder(payDTO);
        return resultData;
    }

    @GetMapping("/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData resultData = null;

        try
        {
            System.out.println("调用开始-----:"+ DateUtil.now());
            resultData = payFeignApi.getPayInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("调用结束-----:"+DateUtil.now());
            ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }

        resultData = payFeignApi.getPayInfo(id);
        return resultData;
    }

    // todo:删除+修改操作作为家庭作业，O(∩_∩)O。。。。。。。

    @GetMapping(value = "/feign/pay/get/info")
    private String getInfoByConsul() {
        return payFeignApi.mylb();
    }

    /**
     * openfeign天然支持负载均衡演示
     *
     * @return
     */
    @GetMapping(value = "/feign/pay/mylb")
    public String mylb() {
        return payFeignApi.mylb();
    }
}
 